/*
 * Player.h
 *
 *  Created on: Oct 22, 2012
 *      Author: default
 */

#ifndef PLAYER_H_
#define PLAYER_H_
#include <MAUtil/Moblet.h>
#include <mastdlib.h>
#include <NativeUI/Widgets.h>
#include <ma.h>
#include <mavsprintf.h>
#include <NativeUI/WidgetUtil.h>
#include <MAUtil/Graphics.h>
#include "MAHeaders.h"
#include "objects.h"
#include <conprint.h>


using namespace MAUtil;
using namespace NativeUI;


class Player : public MAUtil::TimerListener
{
	private:
		int Loc_x;          // The character’s location on the x-axis
		int Loc_y;          // The character’s location on the y-axis
		int Spd_x;          // The character’s speed in the x direction
		int Spd_y;          // The character’s speed in the y direction
		int Acc_x;          // The character’s acceleration in the x direction
		int Acc_y;          // The character’s acceleration in the y direction
		int Energy;         // The character’s energy which determines if he can accelerate
		int ArmorColor;          // 0 = red, 1 = blue
		bool Jumping;   // 0 = no, 1 = yes
		bool Sliding;     // 0 = no, 1 = yes
		bool collision;
		int Sp_Width;
		int Sp_Height;
		int Current_Image;
		int current_collision;
		int total_collision;
		int current_sliding;
		int total_sliding;
		int Total_Width;
		int jump_counter;
		int sprite_speed;
		int sprite_count;
		int score;
		char scoreString[255];
		int orbColor;

		Camera* cam;





		MAExtent screenSize;
		MARect flatman_srcRect, bg_srcRect;
		MAPoint2d flatman_dstPt;




	protected:


	public:
		Player(Camera* camera)
		{
			screenSize = maGetScrSize();
			bg_srcRect.width = EXTENT_Y(screenSize);
			bg_srcRect.height = EXTENT_X(screenSize);
			Loc_x = 150;
			//Loc_y = 15;
			//Loc_x = (bg_srcRect.width/10);
			Loc_y = (bg_srcRect.height-100);
			Spd_x = 5;
			Spd_y = 0;
			Acc_x = 0;
			Acc_y = 0;
			Energy = 100;
			ArmorColor = 0;
			Jumping = 0;
			Sliding = 0;
			collision = false;
			jump_counter = 0;
			sprite_speed = 11;
			sprite_count = 0;
			score =0;

			Sp_Width = 54;
			Sp_Height = 50;
			Total_Width = 162;

			Current_Image = 0;
			current_collision = 0;
			total_collision = 432;
			current_sliding = 0;
			total_sliding = 324;

			cam = camera;


		}

		int GetLoc_x()
		{
			return Loc_x;
		}

		int GetLoc_y()
		{
			return Loc_y;
		}

		int GetSpd_x()
		{
			return Spd_x;
		}

		int GetSpd_y()
		{
			return Spd_y;
		}

		int GetAcc_x()
		{
			return Acc_x;
		}

		int GetAcc_y()
		{
			return Acc_y;
		}

		int GetEnergy()
		{
			return Energy;
		}

		int GetArmor()
		{
			return ArmorColor;
		}

		int GetJumping()
		{
			return Jumping;
		}

		int GetSliding()
		{
			return Sliding;
		}

		void SetLoc_x(int lx)
		{
			Loc_x = lx;
		}

		void SetLoc_y(int ly)
		{
			Loc_y = ly;
		}

		void SetSpd_x(int sx)
		{
			Spd_x = sx;
		}

		void SetSpd_y(int sy)
		{
			Spd_y = sy;
		}

		void SetAcc_x(int ax)
		{
			Acc_x = ax;
		}

		void SetAcc_y(int ay)
		{
			Acc_y = ay;
		}

		void SetEnergy(int e)
		{
			Energy = e;
		}

		void SetArmor(int armor)
		{
			ArmorColor = armor;
		}

		void SetJumping(bool jump)
		{
			Jumping = jump;
		}

		void SetSliding(bool slide)
		{
			Sliding = slide;
		}

		void IsJumping()
		{
			Jumping = true;
		}

		void NotJumping()
		{
			Jumping = false;
		}

		void IsSliding()
		{
			Sliding = true;
		}

		void NotSliding()
		{
			Sliding = false;
		}

		bool IsOnGround()
		{

			if (GetLoc_y() == (bg_srcRect.height-50))
				return true;
			else
				return false;
		}

		void iscollision()
		{
			collision = true;
		}
		void notcollision()
		{
			collision = false;
		}

		void orbcollide(int c)
		{
			collision = true;
			orbColor = c;
		}



		void draw()
			{

			score++;
				if(score % 100 == 0)
				{
					if(Spd_x < 10)
						Spd_x++;
				}
				sprintf(scoreString, "%s %d", "SCORE: ",score);
				Gfx_drawText(5,10, scoreString);

				flatman_srcRect.left = 0;
			    flatman_srcRect.top = 0;
			    flatman_srcRect.width = Sp_Width;
			    flatman_srcRect.height = Sp_Height;
			    flatman_srcRect.left = Current_Image;
			    sprite_count++;
			    if(sprite_count == (sprite_speed - Spd_x))
			    {
			    	Current_Image += Sp_Width;
			    	sprite_count = 0;
			    }
			    flatman_dstPt.x = Loc_x;
			    flatman_dstPt.y = Loc_y;

			   // int i_cam = Loc_x;
			    if(Jumping == false)
				{
					Loc_x += Spd_x;
					Loc_y -= Spd_y;
				}
				else if(Jumping == true) // formula 50 up 15 across -(x - (x + 7))^2 + 50
				{

					Loc_x += Spd_x;
					Loc_y += Spd_y;
					if (Spd_y != -14)/*GetLoc_x() != (mScreenHeight/10)*8*/
						Spd_y++;
					else
					{
						Spd_y--;
						NotJumping();
					}
					/*Loc_y = -(Loc_x -  7)^2 + 50;
					if (Loc_y <= mScreenWidth - 30)
					{
						Loc_y = mScreenWidth - 30;
						NotJumping();
					}*/
				}

		    	flatman_dstPt.x = flatman_dstPt.x - cam->xOffset();
			    if(collision == true)
			    {
			    	flatman_srcRect.left = current_collision;
			    	current_collision += Sp_Width;
			    	if (ArmorColor = 0)
			    		Gfx_drawImageRegion(COLLISION_RED, &flatman_srcRect, &flatman_dstPt, TRANS_NONE);
			    	else
			    		Gfx_drawImageRegion(COLLISION_BLUE, &flatman_srcRect, &flatman_dstPt, TRANS_NONE);
			    	if(current_collision >= total_collision)
			    	{
			    		current_collision = 0;
			    		collision = false;
			    	}
			    }
			    else
			    {
			    	if (ArmorColor == 0)
			    		Gfx_drawImageRegion(FLATMAN_RED, &flatman_srcRect, &flatman_dstPt, TRANS_NONE);
			    	if (ArmorColor == 1)
			    		Gfx_drawImageRegion(FLATMAN_BLUE, &flatman_srcRect, &flatman_dstPt, TRANS_NONE);

			    	if(Current_Image >= (Total_Width))
			    	{
			    		Current_Image = 0;

			    	}
			    }
			    //Gfx_updateScreen();
			    //i_cam = Loc_x - i_cam;
			    //cam->camIncrement(i_cam);


			}

		void runTimerEvent()
			{/*
			if(Jumping == false)
			{
				Loc_x += Spd_x;
				Loc_y += Spd_y;
			}
			else if(Jumping == true) // formula 50 up 15 across -(x - (x + 7))^2 + 50
			{

				Loc_x += Spd_x;
				Loc_y -= Spd_y;
				if (Spd_y != -14)GetLoc_x() != (mScreenHeight/10)*8
					Spd_y--;
				else
				{
					Spd_y = 0;
					NotJumping();
				}*/


				/*Loc_y = -(Loc_x -  7)^2 + 50;
				if (Loc_y <= mScreenWidth - 30)
				{
					Loc_y = mScreenWidth - 30;
					NotJumping();
				}*/
			//}

				/*Loc_x += Spd_x;
				Loc_y += Spd_y;*/



				// Call the function that draws the background and sprites.
				//draw();

			}

};


#endif /* PLAYER_H_ */
